#include "mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(800,600);

    // QWidget *window = new QWidget;
    btDevice = new BTDevice();
    btn_bluetooth.setEnabled(false);
    btn_preset_export.setEnabled(false);
    btn_preset_1.setEnabled(false);
    btn_preset_2.setEnabled(false);
    btn_preset_3.setEnabled(false);
    btn_preset_4.setEnabled(false);
    btn_preset_save.setEnabled(false);
    btn_preset_export.setEnabled(false);

    btn_bluetooth.setText(tr("Search bluetooth device"));
    btn_searial.setText(tr("连接设备"));
    lab_device_info.setText(tr(""));
    btn_export_data.setText(tr("导出数据"));
    group_eq.setTitle(tr("EQ"));
    group_eq_pizeo.setTitle(tr("Pizeo"));
    group_eq_mic.setTitle(tr("Mic"));

    btn_preset_1.setText(tr("预设1"));
    btn_preset_2.setText(tr("预设2"));
    btn_preset_3.setText(tr("预设3"));
    btn_preset_4.setText(tr("预设4"));
    btn_preset_save.setText(tr("保存"));
    btn_preset_export.setText(tr("导出"));
    btn_preset_1.setStyleSheet("background-color: gray");
    btn_preset_2.setStyleSheet("background-color: gray");
    btn_preset_3.setStyleSheet("background-color: gray");
    btn_preset_4.setStyleSheet("background-color: gray");

    status_bar.addWidget(&btn_searial);
    status_bar.addWidget(&btn_bluetooth);
    status_bar.addWidget(&lab_device_info);
    status_bar.showMessage(tr("Ready"));
    status_bar.addPermanentWidget(&lab_device_info,0);
    status_bar.addPermanentWidget(&btn_bluetooth,0);
    status_bar.addPermanentWidget(&btn_searial,0);
    setStatusBar(&status_bar);

    connect(&btn_bluetooth,&QPushButton::clicked,this,&MainWindow::on_btn_click_bluetooth);
    connect(&btn_searial,&QPushButton::clicked,this,&MainWindow::on_btn_click_searial);


    search_device = new SearchDevice(btDevice);
    connect(search_device,&SearchDevice::device_is_connect,this,&MainWindow::on_device_is_connect);
    serialPort = new SerialPortThread();

    connect(serialPort,&SerialPortThread::receiveData,&package_proc,&data_package::receive_data);
    connect(serialPort,&SerialPortThread::sig_communication_connection,this,&MainWindow::on_communication_connection);
    connect(serialPort,&SerialPortThread::sig_communication_disconnect,this,&MainWindow::on_communication_disconnect);
    connect(&package_proc,&data_package::sig_send_data,serialPort,&SerialPortThread::on_send_data);
    connect(&package_proc,&data_package::sig_command_receive,this,&MainWindow::on_command_analyze);

    eqGroupMain.addFilter(EqualizersGroup::Lowshelf,50,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Highshelf,10000,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Peak,100,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Peak,500,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Peak,1000,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Peak,3000,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Peak,5000,0,0.707);
    eqGroupMain.addFilter(EqualizersGroup::Peak,8000,0,0.707);
    eqGroupMain.setEqualizerGroupSampleRate(48000);
    eqGroupMain.setFilterCenterFrequencyMaxAll(24000);
    eqGroupMain.setFilterCenterFrequencyMinAll(20);
    eqGroupMain.setFilterGainDBMaxAll(24);
    eqGroupMain.setFilterGainDBMinAll(-24);
    eqGroupMain.setFilterQMaxAll(10);
    eqGroupMain.setFilterQMinAll(0.005);

    eqGroupMic.addFilter(EqualizersGroup::Lowshelf,50,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Highshelf,10000,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Peak,100,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Peak,500,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Peak,1000,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Peak,3000,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Peak,5000,0,0.707);
    eqGroupMic.addFilter(EqualizersGroup::Peak,8000,0,0.707);
    eqGroupMic.setEqualizerGroupSampleRate(48000);
    eqGroupMic.setFilterCenterFrequencyMaxAll(24000);
    eqGroupMic.setFilterCenterFrequencyMinAll(20);
    eqGroupMic.setFilterGainDBMaxAll(24);
    eqGroupMic.setFilterGainDBMinAll(-24);
    eqGroupMic.setFilterQMaxAll(10);
    eqGroupMic.setFilterQMinAll(0.005);

    eqViewMain = new EqualizerView(&eqGroupMain);
    eqViewMic = new EqualizerView(&eqGroupMic);
    lab_eq_pizeo_cf.setText(tr("频率:"));
    lab_eq_pizeo_q.setText(tr("Q:"));
    lab_eq_pizeo_gain.setText(tr("增益:"));
    txt_eq_pizeo_cf.setText(tr("200"));
    txt_eq_pizeo_q.setText(tr("0.757"));
    txt_eq_pizeo_gain.setText(tr("0"));
    btn_eq_clean_pizeo.setText(tr("增益归零"));
    lab_eq_mic_cf.setText(tr("频率:"));
    lab_eq_mic_q.setText(tr("Q:"));
    lab_eq_mic_gain.setText(tr("增益:"));
    txt_eq_mic_cf.setText(tr("1000"));
    txt_eq_mic_q.setText(tr("1.757"));
    txt_eq_mic_gain.setText(tr("5"));
    btn_eq_clean_mic.setText(tr("增益归零"));
    layout_eq_pizeo_ctrl_cf.addWidget(&lab_eq_pizeo_cf);
    layout_eq_pizeo_ctrl_cf.addWidget(&txt_eq_pizeo_cf);
    layout_eq_pizeo_ctrl_q.addWidget(&lab_eq_pizeo_q);
    layout_eq_pizeo_ctrl_q.addWidget(&txt_eq_pizeo_q);
    layout_eq_pizeo_ctrl_gain.addWidget(&lab_eq_pizeo_gain);
    layout_eq_pizeo_ctrl_gain.addWidget(&txt_eq_pizeo_gain);
    layout_eq_pizeo_ctrl.addLayout(&layout_eq_pizeo_ctrl_cf,10);
    layout_eq_pizeo_ctrl.addLayout(&layout_eq_pizeo_ctrl_q,10);
    layout_eq_pizeo_ctrl.addLayout(&layout_eq_pizeo_ctrl_gain,10);
    layout_eq_pizeo_ctrl.addWidget(&btn_eq_clean_pizeo,10);
    layout_eq_pizeo_ctrl.addStretch(60);

    layout_eq_pizeo.addWidget(eqViewMain,0,0,1,1);
    layout_eq_pizeo.addLayout(&layout_eq_pizeo_ctrl,0,1,1,1);
    layout_eq_pizeo.setRowStretch(0,90);
    layout_eq_pizeo.setRowStretch(1,10);
    layout_eq_pizeo.setColumnStretch(0,90);
    layout_eq_pizeo.setColumnStretch(1,10);
    group_eq_pizeo.setLayout(&layout_eq_pizeo);

    //mic
    layout_eq_mic_ctrl_cf.addWidget(&lab_eq_mic_cf);
    layout_eq_mic_ctrl_cf.addWidget(&txt_eq_mic_cf);
    layout_eq_mic_ctrl_q.addWidget(&lab_eq_mic_q);
    layout_eq_mic_ctrl_q.addWidget(&txt_eq_mic_q);
    layout_eq_mic_ctrl_gain.addWidget(&lab_eq_mic_gain);
    layout_eq_mic_ctrl_gain.addWidget(&txt_eq_mic_gain);
    layout_eq_mic_ctrl.addLayout(&layout_eq_mic_ctrl_cf,10);
    layout_eq_mic_ctrl.addLayout(&layout_eq_mic_ctrl_q,10);
    layout_eq_mic_ctrl.addLayout(&layout_eq_mic_ctrl_gain,10);
    layout_eq_mic_ctrl.addWidget(&btn_eq_clean_mic,10);
    layout_eq_mic_ctrl.addStretch(60);

    layout_eq_mic.addWidget(eqViewMic,0,0,1,1);
    layout_eq_mic.addLayout(&layout_eq_mic_ctrl,0,1,1,1);
    layout_eq_mic.setRowStretch(0,90);
    layout_eq_mic.setRowStretch(1,10);
    layout_eq_mic.setColumnStretch(0,90);
    layout_eq_mic.setColumnStretch(1,10);
    group_eq_mic.setLayout(&layout_eq_mic);
    layout_eq.addWidget(&group_eq_pizeo,0,0,1,1);
    layout_eq.addWidget(&group_eq_mic,1,0,1,1);
    layout_eq.setRowStretch(0,50);
    layout_eq.setRowStretch(1,50);
    group_eq.setLayout(&layout_eq);

    layout_main.addWidget(&btn_preset_1,0,0,1,1);
    layout_main.addWidget(&btn_preset_2,0,1,1,1);
    layout_main.addWidget(&btn_preset_3,0,2,1,1);
    layout_main.addWidget(&btn_preset_4,0,3,1,1);
    layout_main.addWidget(&btn_preset_save,0,4,1,1);
    layout_main.addWidget(&btn_preset_export,0,5,1,1);
    layout_main.addWidget(&group_eq,1,0,1,6);
    setCentralWidget(&window);
    window.setLayout(&layout_main);

    connect(eqViewMain,&EqualizerView::sig_eq_select_change,this,&MainWindow::on_eq_pizeo_select_change);
    connect(eqViewMain,&EqualizerView::sig_eq_select_release,this,&MainWindow::on_eq_pizeo_select_release);
    connect(eqViewMic,&EqualizerView::sig_eq_select_change,this,&MainWindow::on_eq_mic_select_change);
    connect(eqViewMic,&EqualizerView::sig_eq_select_release,this,&MainWindow::on_eq_mic_select_release);
    connect(&btn_eq_clean_pizeo,&QPushButton::clicked,this,&MainWindow::on_btn_click_eq_clean_pizeo);
    connect(&btn_eq_clean_mic,&QPushButton::clicked,this,&MainWindow::on_btn_click_eq_clean_mic);
    connect(&txt_eq_pizeo_cf,&QLineEdit::returnPressed,this,&MainWindow::slot_le_textedited);
    connect(&txt_eq_pizeo_q,&QLineEdit::returnPressed,this,&MainWindow::slot_le_textedited);
    connect(&txt_eq_pizeo_gain,&QLineEdit::returnPressed,this,&MainWindow::slot_le_textedited);
    connect(&txt_eq_mic_cf,&QLineEdit::returnPressed,this,&MainWindow::slot_le_textedited);
    connect(&txt_eq_mic_q,&QLineEdit::returnPressed,this,&MainWindow::slot_le_textedited);
    connect(&txt_eq_mic_gain,&QLineEdit::returnPressed,this,&MainWindow::slot_le_textedited);
    connect(&btn_preset_1,&QPushButton::clicked,this,&MainWindow::on_btn_click_preset);
    connect(&btn_preset_2,&QPushButton::clicked,this,&MainWindow::on_btn_click_preset);
    connect(&btn_preset_3,&QPushButton::clicked,this,&MainWindow::on_btn_click_preset);
    connect(&btn_preset_4,&QPushButton::clicked,this,&MainWindow::on_btn_click_preset);
    connect(&btn_preset_save,&QPushButton::clicked,this,&MainWindow::on_btn_click_preset_save);
    connect(&btn_preset_export,&QPushButton::clicked,this,&MainWindow::on_btn_click_preset_export);

    // QRegExpValidator *regIntValidator = new QRegExpValidator(QRegExp("\\d+"), this);
    QIntValidator *regIntValidator = new QIntValidator(20, 24000, this);
    txt_eq_pizeo_cf.setValidator(regIntValidator);
    QDoubleValidator *regDubValidator = new QDoubleValidator(-24.0, 24.0, 2, this);
    txt_eq_pizeo_gain.setValidator(regDubValidator);
    QDoubleValidator *regDub2Validator = new QDoubleValidator(0.0, 10.0, 3, this);
    txt_eq_pizeo_q.setValidator(regDub2Validator);
    QIntValidator *micregIntValidator = new QIntValidator(20, 24000, this);
    txt_eq_mic_cf.setValidator(micregIntValidator);
    QDoubleValidator *micregDubValidator = new QDoubleValidator(-24.0, 24.0, 2, this);
    txt_eq_mic_gain.setValidator(micregDubValidator);
    QDoubleValidator *micregDub2Validator = new QDoubleValidator(0.0, 10.0, 3, this);
    txt_eq_mic_q.setValidator(micregDub2Validator);

    eq_pizeo_current_select = 0;
    eq_mic_current_select = 0;

//    on_update_device_info(1,1,0,"test");
//    on_btn_click_bluetooth();

//    unsigned char buff[4]={0x00,0x80,0x7c,0x43};
//    float *pf = (float *)buff;
//    qDebug() << "float:" << *pf;
}

MainWindow::~MainWindow()
{
    if(btDevice != nullptr)
    {
        btDevice->disconnectFromDevice();
        delete btDevice;
    }
    if(search_device != nullptr)
    {
        delete search_device;
    }
    if(serialPort != nullptr)
    {
        delete serialPort;
    }
}

void MainWindow::send_command(uint8_t *command_data, size_t bytes)
{
    QByteArray package_data;
    package_proc.create_data_package(command_data,bytes,package_data);
    serialPort->serialWriteData(package_data);
}

void MainWindow::on_btn_click_bluetooth()
{
//    search_device = new SearchDevice(btDevice);
//    search_device->setAttribute(Qt::WA_DeleteOnClose,true);
//    search_device->setWindowModality(Qt::ApplicationModal);
//    search_device->show();
    search_device->setWindowTitle(tr("Search Devices..."));
    search_device->exec();
}

void MainWindow::on_btn_click_searial()
{
    serialPort->setWindowTitle(tr("open searial port"));
    serialPort->exec();
}

void MainWindow::on_device_is_connect()
{
    // QByteArray data;
    // data.append((char)0x55);
    // data.append((char)0xAA);
    // data.append((char)0x0C);
    // data.append((char)0x09);
    // data.append((char)0x00);
    // data.append((char)0x00);
    // data.append((char)0x15);
    // data.append((char)0x00);
    // search_device->write_data_to_device(data);
}

void MainWindow::update_device_info(uint8_t verf_hw, uint8_t verf_main, uint8_t verf_modify, QString name)
{
    lab_device_info.setText(tr("硬件:") + QString::number(verf_hw) + tr(", 固件:") + QString::number(verf_main) + tr(".") + QString::number(verf_modify) + tr(",设备名称:") + name);
    serialPort->hide();

    preset_select_index = 0;
    max_preset = 0;

    // QString name_K13 = "K13";
    // QString name_SG1_Pro = "SG1 Pro";
    if(name.compare("K13",Qt::CaseSensitive) == 0)
    {
        max_preset = 4;
        btn_preset_1.setEnabled(true);
        btn_preset_2.setEnabled(true);
        btn_preset_3.setEnabled(true);
        btn_preset_4.setEnabled(true);

    }
    else if(name.compare("SG1 Pro",Qt::CaseSensitive) == 0)
    {
        max_preset = 1;
        btn_preset_1.setEnabled(true);
        btn_preset_2.setEnabled(false);
        btn_preset_3.setEnabled(false);
        btn_preset_4.setEnabled(false);
    }
    else
    {
        return ;
    }
    btn_preset_1.setStyleSheet("background-color: gray");
    btn_preset_2.setStyleSheet("background-color: gray");
    btn_preset_3.setStyleSheet("background-color: gray");
    btn_preset_4.setStyleSheet("background-color: gray");
    btn_preset_1.setStyleSheet("background-color: green");
    btn_preset_save.setEnabled(true);
    btn_preset_export.setEnabled(true);

    for(int i=0; i<max_preset; i++)
    {
        command_get_preset(i);
        QThread::msleep(10);
    }

    btn_preset_export.setEnabled(true);

    // uint8_t buffer[3];
    // for (int i = 0; i < 16; ++i) {
    //     buffer[0]=CMD_INDEX_EFFECT_EQ;
    //     buffer[1]=(char)i;
    //     send_command(buffer,2);
    //     QThread::msleep(10);
    // }
}

void MainWindow::send_param_to_device_eq(int index)
{
    uint8_t buffer[64];

    uint16_t type=1;
    double cf=0,q=0.747,g=0;
    int16_t param_buffer[16];
    if(index < 0)
    {
        return;
    }
    if(index < 8)
    {
        type = eqGroupMain.getFilterType(index);
        cf = eqGroupMain.getFilterCenterFrequency(index);
        q  = eqGroupMain.getFilterQ(index);
        g  = eqGroupMain.getFilterGainDB(index);
    }
    else if(index < 16)
    {
        int micIndex = index - 8;
        type = eqGroupMic.getFilterType(micIndex);
        cf = eqGroupMic.getFilterCenterFrequency(micIndex);
        q  = eqGroupMic.getFilterQ(micIndex);
        g  = eqGroupMic.getFilterGainDB(micIndex);
    }
    param_buffer[0] = (int16_t)type;
    param_buffer[1] = (int16_t)cf;
    param_buffer[2] = (int16_t)(g * 1000);
    param_buffer[3] = (int16_t)(q * 1000);
    buffer[0] = CMD_INDEX_EFFECT_EQ;
    buffer[1] = (char)index | 0x80;
    buffer[2] = (char)preset_select_index;
    ::memcpy((void *)&buffer[3],(void *)&param_buffer[0],8);
    send_command(buffer,3+8);

}

void MainWindow::command_get_preset(int index)
{
    uint8_t buffer[3];
    for (int i = 0; i < 16; ++i) {
        buffer[0]=CMD_INDEX_EFFECT_EQ;
        buffer[1]=(char)i;
        buffer[2]=(char)index;
        send_command(buffer,3);
        QThread::msleep(10);
    }

}

void MainWindow::on_communication_connection(bool status)
{
    if(status == true)
    {
        uint8_t buffer[3];
        buffer[0]=CMD_INDEX_SYSTEM;
        buffer[1]=(char)SYSTEM_PARAM_INDEX_INFO;
        send_command(buffer,2);
    }
}

void MainWindow::on_communication_disconnect()
{
    lab_device_info.setText(tr(""));
}

void MainWindow::on_command_analyze(QByteArray data)
{
    uint8_t cmd_index;
    uint8_t set_param_mark;
    uint8_t param_index;
    uint8_t cmd_data_buffer[512];
    uint8_t *data_buff = (uint8_t *)data.data();

    cmd_index = data_buff[0];
    switch(cmd_index)
    {
    case CMD_INDEX_SYSTEM:
    {
        set_param_mark = (data_buff[1] & 0x80) ? 1 : 0;
        param_index = data_buff[1] & 0x7F;
        uint8_t *param_buffer = &data_buff[2];
        if(param_index == SYSTEM_PARAM_INDEX_INFO)
        {
            if(set_param_mark)
            {
                uint8_t verf_hw;
                uint8_t verf_main;
                uint8_t verf_modify;
                verf_hw = param_buffer[0];
                verf_main = param_buffer[1];
                verf_modify = param_buffer[2];
                QString name = QString((const char *)&param_buffer[3]);
                update_device_info(verf_hw,verf_main,verf_modify,name);
            }
            else
            {
            }
        }
        else
        {

        }
    }
    break;
    case CMD_INDEX_EFFECT_REVERB :
    {
        set_param_mark = (data_buff[1] & 0x80) ? 1 : 0;
        param_index = data_buff[1] & 0x7F;
        uint8_t *param_buffer = &data_buff[2];
        if(param_index == 0)
        {
            if(set_param_mark)
            {
                int16_t param;
                cmd_effect_params.rev_enable = param_buffer[0] > 0 ? 1 : 0;
                param  = param_buffer[1];
                param |= (uint16_t)param_buffer[2] << 8;
                cmd_effect_params.rev_dry = param;
                param  = param_buffer[3];
                param |= (uint16_t)param_buffer[4] << 8;
                cmd_effect_params.rev_wet = param;
                param  = param_buffer[5];
                param |= (uint16_t)param_buffer[6] << 8;
                cmd_effect_params.rev_damp = param;
                param  = param_buffer[7];
                param |= (uint16_t)param_buffer[8] << 8;
                cmd_effect_params.rev_tone = param;
                param  = param_buffer[9];
                param |= (uint16_t)param_buffer[10] << 8;
                cmd_effect_params.rev_fadeback = param;
            }
            else
            {
                int16_t params[10];
                params[0] = (int16_t)cmd_effect_params.rev_enable;
                params[1] = (int16_t)cmd_effect_params.rev_dry;
                params[2] = (int16_t)cmd_effect_params.rev_wet;
                params[3] = (int16_t)cmd_effect_params.rev_damp;
                params[4] = (int16_t)cmd_effect_params.rev_tone;
                params[5] = (int16_t)cmd_effect_params.rev_fadeback;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)params[0];
                cmd_data_buffer[3]  = (uint8_t)(params[1]      & 0x00FF);
                cmd_data_buffer[4]  = (uint8_t)(params[1] >> 8 & 0x00FF);
                cmd_data_buffer[5]  = (uint8_t)(params[2]      & 0x00FF);
                cmd_data_buffer[6]  = (uint8_t)(params[2] >> 8 & 0x00FF);
                cmd_data_buffer[7]  = (uint8_t)(params[3]      & 0x00FF);
                cmd_data_buffer[8]  = (uint8_t)(params[3] >> 8 & 0x00FF);
                cmd_data_buffer[9]  = (uint8_t)(params[4]      & 0x00FF);
                cmd_data_buffer[10]  = (uint8_t)(params[4] >> 8 & 0x00FF);
                cmd_data_buffer[11]  = (uint8_t)(params[5]      & 0x00FF);
                cmd_data_buffer[12] = (uint8_t)(params[5] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,13);
            }
        }
        else if(param_index == 1)
        {
            if(set_param_mark)
            {
                cmd_effect_params.rev_enable = param_buffer[0] > 0 ? 1 : 0;
            }
            else
            {
                cmd_data_buffer[0]  = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)cmd_effect_params.rev_enable;
                send_command(cmd_data_buffer,3);
            }
        }
        else if(param_index == 2)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_dry = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_dry;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 3)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_wet = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_wet;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 4)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_damp = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_damp;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 5)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_tone = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_tone;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 6)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_fadeback = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_fadeback;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
    }
    break;
    case CMD_INDEX_EFFECT_REVERB_MIC :
    {
        set_param_mark = (data_buff[1] & 0x80) ? 1 : 0;
        param_index = data_buff[1] & 0x7F;
        uint8_t *param_buffer = &data_buff[2];
        if(param_index == 0)
        {
            if(set_param_mark)
            {
                int16_t param;
                cmd_effect_params.rev_mic_enable = param_buffer[0] > 0 ? 1 : 0;
                param  = param_buffer[1];
                param |= (uint16_t)param_buffer[2] << 8;
                cmd_effect_params.rev_mic_dry = param;
                param  = param_buffer[3];
                param |= (uint16_t)param_buffer[4] << 8;
                cmd_effect_params.rev_mic_wet = param;
                param  = param_buffer[5];
                param |= (uint16_t)param_buffer[6] << 8;
                cmd_effect_params.rev_mic_damp = param;
                param  = param_buffer[7];
                param |= (uint16_t)param_buffer[8] << 8;
                cmd_effect_params.rev_mic_tone = param;
                param  = param_buffer[9];
                param |= (uint16_t)param_buffer[10] << 8;
                cmd_effect_params.rev_mic_fadeback = param;
            }
            else
            {
                int16_t params[10];
                params[0] = (int16_t)cmd_effect_params.rev_mic_enable;
                params[1] = (int16_t)cmd_effect_params.rev_mic_dry;
                params[2] = (int16_t)cmd_effect_params.rev_mic_wet;
                params[3] = (int16_t)cmd_effect_params.rev_mic_damp;
                params[4] = (int16_t)cmd_effect_params.rev_mic_tone;
                params[5] = (int16_t)cmd_effect_params.rev_mic_fadeback;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t) params[0];
                cmd_data_buffer[3]  = (uint8_t)(params[1]      & 0x00FF);
                cmd_data_buffer[4]  = (uint8_t)(params[1] >> 8 & 0x00FF);
                cmd_data_buffer[5]  = (uint8_t)(params[2]      & 0x00FF);
                cmd_data_buffer[6]  = (uint8_t)(params[2] >> 8 & 0x00FF);
                cmd_data_buffer[7]  = (uint8_t)(params[3]      & 0x00FF);
                cmd_data_buffer[8]  = (uint8_t)(params[3] >> 8 & 0x00FF);
                cmd_data_buffer[9]  = (uint8_t)(params[4]      & 0x00FF);
                cmd_data_buffer[10]  = (uint8_t)(params[4] >> 8 & 0x00FF);
                cmd_data_buffer[11] = (uint8_t)(params[5]      & 0x00FF);
                cmd_data_buffer[12] = (uint8_t)(params[5] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,13);
            }
        }
        else if(param_index == 1)
        {
            if(set_param_mark)
            {
                cmd_effect_params.rev_mic_enable = param_buffer[0] > 0 ? 1 : 0;
            }
            else
            {
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)cmd_effect_params.rev_mic_enable;
                send_command(cmd_data_buffer,3);
            }
        }
        else if(param_index == 2)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_mic_dry = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_mic_dry;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 3)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_mic_wet = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_mic_wet;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 4)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_mic_damp = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_mic_damp;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 5)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_mic_tone = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_mic_tone;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 6)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.rev_mic_fadeback = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.rev_mic_fadeback;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
    }
    break;
    case CMD_INDEX_EFFECT_DELAY :
    {
        set_param_mark = (data_buff[1] & 0x80) ? 1 : 0;
        param_index = data_buff[1] & 0x7F;
        uint8_t *param_buffer = &data_buff[2];
        if(param_index == 0)
        {
            if(set_param_mark)
            {
                int16_t param;
                cmd_effect_params.dly_enable = param_buffer[0] > 0 ? 1 : 0;
                param  = param_buffer[1];
                param |= (uint16_t)param_buffer[2] << 8;
                cmd_effect_params.dly_time_ms = param;
                param  = param_buffer[3];
                param |= (uint16_t)param_buffer[4] << 8;
                cmd_effect_params.dly_mix = param;
                param  = param_buffer[5];
                param |= (uint16_t)param_buffer[6] << 8;
                cmd_effect_params.dly_tone = param;
                param  = param_buffer[7];
                param |= (uint16_t)param_buffer[8] << 8;
                cmd_effect_params.dly_fadeback = param;
            }
            else
            {
                int16_t params[5];
                params[0] = (int16_t)cmd_effect_params.dly_enable;
                params[1] = (int16_t)cmd_effect_params.dly_time_ms;
                params[2] = (int16_t)cmd_effect_params.dly_mix;
                params[3] = (int16_t)cmd_effect_params.dly_tone;
                params[4] = (int16_t)cmd_effect_params.dly_fadeback;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)params[0];
                cmd_data_buffer[3]  = (uint8_t)(params[1]      & 0x00FF);
                cmd_data_buffer[4]  = (uint8_t)(params[1] >> 8 & 0x00FF);
                cmd_data_buffer[5]  = (uint8_t)(params[2]      & 0x00FF);
                cmd_data_buffer[6]  = (uint8_t)(params[2] >> 8 & 0x00FF);
                cmd_data_buffer[7]  = (uint8_t)(params[3]      & 0x00FF);
                cmd_data_buffer[8]  = (uint8_t)(params[3] >> 8 & 0x00FF);
                cmd_data_buffer[9]  = (uint8_t)(params[4]      & 0x00FF);
                cmd_data_buffer[10]  = (uint8_t)(params[4] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,11);
            }
        }
        else if(param_index == 1)
        {
            if(set_param_mark)
            {
                cmd_effect_params.dly_enable = param_buffer[0] > 0 ? 1 : 0;
            }
            else
            {
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)cmd_effect_params.dly_enable;
                send_command(cmd_data_buffer,3);
            }
        }
        else if(param_index == 2)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.dly_time_ms = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.dly_time_ms;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 3)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.dly_mix = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.dly_mix;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 4)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.dly_tone = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.dly_tone;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
        else if(param_index == 5)
        {
            if(set_param_mark)
            {
                int16_t param;
                param  = param_buffer[0];
                param |= (uint16_t)param_buffer[1] << 8;
                cmd_effect_params.dly_fadeback = param;
            }
            else
            {
                int16_t params[1];
                params[0] = (int16_t)cmd_effect_params.dly_fadeback;
                cmd_data_buffer[0] = cmd_index;
                cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
                cmd_data_buffer[2]  = (uint8_t)(params[0]      & 0x00FF);
                cmd_data_buffer[3]  = (uint8_t)(params[0] >> 8 & 0x00FF);
                send_command(cmd_data_buffer,4);
            }
        }
    }
    break;
    case CMD_INDEX_EFFECT_EQ :
    {
        uint8_t preset;
        set_param_mark = (data_buff[1] & 0x80) ? 1 : 0;
        param_index = data_buff[1] & 0x7F;
        preset = data_buff[2];
        uint8_t *param_buffer = &data_buff[3];
        if(set_param_mark)
        {
            EqualizersGroup *eqGroup;
            EqualizerView *eqView;
            __filter_param *eq_param;
            if(param_index < 8)
            {
                eqGroup = &eqGroupMain;
                eqView = eqViewMain;
                eq_param = &preset_param_filter_pizeo[preset][param_index];
            }
            else
            {
                eqGroup = &eqGroupMic;
                eqView = eqViewMic;
                param_index -= 8;
                eq_param = &preset_param_filter_mic[preset][param_index];
            }
            int16_t param;
            EqualizersGroup::FilterType type;
            double cf,g,q;
            param = param_buffer[0];
            param |= (uint16_t)param_buffer[1] << 8;
            type = (EqualizersGroup::FilterType)param;
            param = param_buffer[2];
            param |= (uint16_t)param_buffer[3] << 8;
            cf = param;
            param = param_buffer[4];
            param |= (uint16_t)param_buffer[5] << 8;
            g = (double)param / 1000.0;
            param = param_buffer[6];
            param |= (uint16_t)param_buffer[7] << 8;
            q = (double)param / 1000.0;
            eq_param->type = type;
            eq_param->center_frequency = cf;
            eq_param->gain_db = g;
            eq_param->q = q;
            if(preset_select_index == preset)
            {
                eqGroup->setFilterType(param_index,type);
                eqGroup->setFilterCenterFrequency(param_index,cf);
                eqGroup->setFilterGainDB(param_index,g);
                eqGroup->setFilterQ(param_index,q);
                eqView->updateView();
            }
        }
        else
        {
            // EqualizersGroup *eqGroup;
            // if(param_index < 8)
            // {
            //     eqGroup = &eqGroupMain;
            // }
            // else
            // {
            //     eqGroup = &eqGroupMic;
            //     param_index -= 8;
            // }
            // uint16_t data_buff[8];
            // data_buff[0] = eqGroup->getFilterType(param_index);
            // data_buff[1] = eqGroup->getFilterCenterFrequency(param_index);
            // data_buff[2] = eqGroup->getFilterGainDB(param_index) * 1000;
            // data_buff[3] = eqGroup->getFilterQ(param_index) * 1000;
            // cmd_data_buffer[0] = cmd_index;
            // cmd_data_buffer[1]  = (uint8_t)(0x80 | param_index);
            // memcpy((void *)&cmd_data_buffer[2],(void *)&data_buff[0],8);
            // send_command(cmd_data_buffer,10);
        }
    }
    break;
    default:
    {
    }
    break;
    }
}

void MainWindow::on_eq_pizeo_select_change(int index)
{
    double cf,q,gain;
    cf = eqGroupMain.getFilterCenterFrequency(index);
    q = eqGroupMain.getFilterQ(index);
    gain = eqGroupMain.getFilterGainDB(index);
    txt_eq_pizeo_cf.setText(QString("%1").arg(cf,0,'0',0));
    txt_eq_pizeo_q.setText(QString("%1").arg(q,0,'0',3));
    txt_eq_pizeo_gain.setText(QString("%1").arg(gain,0,'0',2));
    eq_pizeo_current_select = index;
    send_param_to_device_eq(eq_pizeo_current_select);
}

void MainWindow::on_eq_pizeo_select_release(int index)
{
    on_eq_pizeo_select_change(index);
    send_param_to_device_eq(eq_pizeo_current_select);
}

void MainWindow::on_eq_mic_select_change(int index)
{
    double cf,q,gain;
    cf = eqGroupMic.getFilterCenterFrequency(index);
    q = eqGroupMic.getFilterQ(index);
    gain = eqGroupMic.getFilterGainDB(index);
    txt_eq_mic_cf.setText(QString("%1").arg(cf,0,'0',0));
    txt_eq_mic_q.setText(QString("%1").arg(q,0,'0',3));
    txt_eq_mic_gain.setText(QString("%1").arg(gain,0,'0',2));
    eq_mic_current_select = index;
    send_param_to_device_eq(eq_mic_current_select+8);
}

void MainWindow::on_eq_mic_select_release(int index)
{
    on_eq_mic_select_change(index);
    send_param_to_device_eq(eq_mic_current_select+8);
}

void MainWindow::on_btn_click_eq_clean_pizeo()
{
    QMessageBox msgBox;
    msgBox.setText(tr("确定清除所有EQ参数值吗?"));
    // msgBox.setInformativeText(tr("所有EQ参数将会清零"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.setIcon(QMessageBox::Warning);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Yes)
    {
        eqGroupMain.setFilterGainDBAll(0);
        eqViewMain->updateView();
        for(int i=0; i<8; i++)
        {
            send_param_to_device_eq(i);
            QThread::msleep(10);
        }
    }
    else if(ret == QMessageBox::No)
    {

    }
}

void MainWindow::on_btn_click_eq_clean_mic()
{
    QMessageBox msgBox;
    msgBox.setText(tr("确定清除所有EQ参数值吗?"));
    // msgBox.setInformativeText(tr("所有EQ参数将会清零"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.setIcon(QMessageBox::Warning);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Yes)
    {
        eqGroupMic.setFilterGainDBAll(0);
        eqViewMic->updateView();
        for(int i=0; i<8; i++)
        {
            send_param_to_device_eq(i+8);
            QThread::msleep(10);
        }
    }
    else if(ret == QMessageBox::No)
    {

    }
}

//void MainWindow::slot_le_textedited(const QString &text)
void MainWindow::slot_le_textedited(void)
{
    QLineEdit *lineEdit = qobject_cast<QLineEdit *>(sender());
    if(lineEdit == NULL)
    {
        return ;
    }
    if(eq_pizeo_current_select < 0)
    {
        return ;
    }
    if(eq_mic_current_select < 0)
    {
        return ;
    }
    QString text = lineEdit->text();
    if(lineEdit == &txt_eq_pizeo_cf)
    {
        bool isok;
        int cf = text.toInt(&isok,10);
        if(isok)
        {
            if((cf >= 20) && (cf <= 24000))
            {
                qDebug() << "cf range ok";
                eqGroupMain.setFilterCenterFrequency(eq_pizeo_current_select,cf);
                eqViewMain->updateView();
                send_param_to_device_eq(eq_pizeo_current_select);
            }
            else
            {
                lineEdit->selectAll();
            }
        }
        else
        {
            lineEdit->selectAll();
        }
    }
    else if(lineEdit == &txt_eq_pizeo_q)
    {
        bool isok;
        double q = text.toDouble(&isok);
        if(isok)
        {
            if((q >= 0.005) && (q <= 10))
            {
                qDebug() << "q range ok";
                eqGroupMain.setFilterQ(eq_pizeo_current_select,q);
                eqViewMain->updateView();
                send_param_to_device_eq(eq_pizeo_current_select);
            }
            else
            {
                lineEdit->selectAll();
            }
        }
        else
        {
            lineEdit->selectAll();
        }
    }
    else if(lineEdit == &txt_eq_pizeo_gain)
    {
        bool isok;
        double g = text.toDouble(&isok);
        if(isok)
        {
            if((g >= -24) && (g <= 24))
            {
                qDebug() << "gain range ok";
                eqGroupMain.setFilterGainDB(eq_pizeo_current_select,g);
                eqViewMain->updateView();
                send_param_to_device_eq(eq_pizeo_current_select);
            }
            else
            {
                lineEdit->selectAll();
            }
        }
        else
        {
            lineEdit->selectAll();
        }
    }
    else if(lineEdit == &txt_eq_mic_cf)
    {
        bool isok;
        int cf = text.toInt(&isok,10);
        if(isok)
        {
            if((cf >= 20) && (cf <= 24000))
            {
                qDebug() << "cf range ok";
                eqGroupMic.setFilterCenterFrequency(eq_mic_current_select,cf);
                eqViewMic->updateView();
                send_param_to_device_eq(eq_mic_current_select*2);
            }
            else
            {
                lineEdit->selectAll();
            }
        }
        else
        {
            lineEdit->selectAll();
        }
    }
    else if(lineEdit == &txt_eq_mic_q)
    {
        bool isok;
        double q = text.toDouble(&isok);
        if(isok)
        {
            if((q >= 0.005) && (q <= 10))
            {
                qDebug() << "q range ok";
                eqGroupMic.setFilterQ(eq_mic_current_select,q);
                eqViewMic->updateView();
                send_param_to_device_eq(eq_mic_current_select*2);
            }
            else
            {
                lineEdit->selectAll();
            }
        }
        else
        {
            lineEdit->selectAll();
        }
    }
    else if(lineEdit == &txt_eq_mic_gain)
    {
        bool isok;
        double g = text.toDouble(&isok);
        if(isok)
        {
            if((g >= -24) && (g <= 24))
            {
                qDebug() << "gain range ok";
                eqGroupMic.setFilterGainDB(eq_mic_current_select,g);
                eqViewMic->updateView();
                send_param_to_device_eq(eq_mic_current_select*2);
            }
            else
            {
                lineEdit->selectAll();
            }
        }
        else
        {
            lineEdit->selectAll();
        }
    }
}

void MainWindow::on_btn_click_preset()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
    if(btn == NULL)
    {
        return ;
    }
    btn_preset_1.setStyleSheet("background-color: gray");
    btn_preset_2.setStyleSheet("background-color: gray");
    btn_preset_3.setStyleSheet("background-color: gray");
    btn_preset_4.setStyleSheet("background-color: gray");
    if(btn == &btn_preset_1)
    {
        preset_select_index = 0;
        command_get_preset(preset_select_index);
        btn_preset_1.setStyleSheet("background-color: green");
    }
    else if(btn == &btn_preset_2)
    {
        preset_select_index = 1;
        command_get_preset(preset_select_index);
        btn_preset_2.setStyleSheet("background-color: green");
    }
    else if(btn == &btn_preset_3)
    {
        preset_select_index = 2;
        command_get_preset(preset_select_index);
        btn_preset_3.setStyleSheet("background-color: green");
    }
    else if(btn == &btn_preset_4)
    {
        preset_select_index = 3;
        command_get_preset(preset_select_index);
        btn_preset_4.setStyleSheet("background-color: green");
    }
}

void MainWindow::on_btn_click_preset_save()
{
    uint8_t buffer[3];
    buffer[0]=CMD_INDEX_EFFECT_PRESET;
    buffer[1]=(uint8_t)(0x80 | 0x00);
    buffer[2]=(uint8_t)0xff;
    send_command(buffer,3);

    QMessageBox msgBox;
    msgBox.setText(tr("保存成功"));
    // msgBox.setInformativeText(tr("所有EQ参数将会清零"));
    msgBox.setStandardButtons(QMessageBox::Yes);
    msgBox.setIcon(QMessageBox::Information);
    msgBox.exec();

}

const char *filter_type_name_0 = "error";
const char *filter_type_name_1 = "filter_type_lowpass";
const char *filter_type_name_2 = "filter_type_hightpass";
const char *filter_type_name_3 = "filter_type_bandpass";
const char *filter_type_name_4 = "filter_type_peak";
const char *filter_type_name_5 = "filter_type_notch";
const char *filter_type_name_6 = "filter_type_lowshelf";
const char *filter_type_name_7 = "filter_type_highshelf";

const char *filter_type_name[8]={
    filter_type_name_0,
    filter_type_name_1,
    filter_type_name_2,
    filter_type_name_3,
    filter_type_name_4,
    filter_type_name_5,
    filter_type_name_6,
    filter_type_name_7,
};

void MainWindow::on_btn_click_preset_export()
{
    QString out_file_path;

    out_file_path.clear();
    out_file_path = QFileDialog::getSaveFileName(this, tr("导出"),"preset_data.c",tr("source file (*.c *.txt)"));
    qDebug() << "path:" << out_file_path;
    if(!out_file_path.isEmpty())
    {
        QFile file = QFile(out_file_path);
        if(file.open(QIODevice::WriteOnly) == true)
        {
            char out_buff_1[512];
            file.write("\r__align(32) __filter_param eq_param_pizeo[EQ_PRESET_NUM][EQ_BANDS_MAX_PIZEO]=\r{");
            for(int n=0; n<max_preset; n++)
            {
                file.write("\r\t{");
                for(int i=0; i<8; i++)
                {
                    ::sprintf(out_buff_1,"\r\t\t{\r\n\t\t\t.type = %s,\r\n\t\t\t.gain_db = %f,\r\n\t\t\t.q = %f,\r\n\t\t\t.center_frequency = %f,\r\n\t\t},",filter_type_name[(int)preset_param_filter_pizeo[n][i].type],preset_param_filter_pizeo[n][i].gain_db,preset_param_filter_pizeo[n][i].q,::floor(preset_param_filter_pizeo[n][i].center_frequency));
                    // ::sprintf(out_buff_1,"\r\t\t{\r\n\t\t\t.type = %s,\r\n\t\t\t.gain_db = %f,\r\n\t\t\t.q = %f,\r\n\t\t\t.center_frequency = %f,\r\n\t\t},",filter_type_name[eqGroupMain.getFilterType(i)],eqGroupMain.getFilterGainDB(i),eqGroupMain.getFilterQ(i),::floor(eqGroupMain.getFilterCenterFrequency(i)));
                    file.write(out_buff_1);
                }
                file.write("\r\t},");
            }
            file.write("\r};");
            file.write("\r");
            file.write("\r__align(32) __filter_param eq_param_mic[EQ_PRESET_NUM][EQ_BANDS_MAX_MIC]=\r{");
            for(int n=0; n<max_preset; n++)
            {
                file.write("\r\t{");
                for(int i=0; i<8; i++)
                {
                    ::sprintf(out_buff_1,"\r\t\t{\r\n\t\t\t.type = %s,\r\n\t\t\t.gain_db = %f,\r\n\t\t\t.q = %f,\r\n\t\t\t.center_frequency = %f,\r\n\t\t},",filter_type_name[(int)preset_param_filter_mic[n][i].type],preset_param_filter_mic[n][i].gain_db,preset_param_filter_mic[n][i].q,::floor(preset_param_filter_mic[n][i].center_frequency));
                    // ::sprintf(out_buff_1,"\r\t\t{\r\n\t\t\t.type = %s,\r\n\t\t\t.gain_db = %f,\r\n\t\t\t.q = %f,\r\n\t\t\t.center_frequency = %f,\r\n\t\t},",filter_type_name[eqGroupMic.getFilterType(i)],eqGroupMic.getFilterGainDB(i),eqGroupMic.getFilterQ(i),::floor(eqGroupMic.getFilterCenterFrequency(i)));
                    file.write(out_buff_1);
                }
                file.write("\r\t},");
            }
            file.write("\r};");

            file.close();
        }
    }
}

